package frontend.admin;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class UserManagementPanel extends JPanel {
    private final UserTableComponents tableComponents;

    public UserManagementPanel() {
        setLayout(new BorderLayout());

        // 初始化表格组件
        tableComponents = new UserTableComponents();

        // 表格面板
        add(tableComponents.getScrollPane(), BorderLayout.CENTER);

        // 添加按钮（右下角）
        JButton addButton = new JButton("添加用户");
        addButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        addButton.setBackground(new Color(70, 130, 180));
        addButton.setForeground(Color.WHITE);
        addButton.setFocusPainted(false);
        addButton.setBorderPainted(false); // 禁用边框
        addButton.setContentAreaFilled(false); // 禁用默认背景
        addButton.setOpaque(true); // 允许背景色生效
        addButton.addActionListener(this::showAddUserDialog);

        JPanel buttonWrapper = new JPanel(new FlowLayout(FlowLayout.RIGHT, 10, 10));
        buttonWrapper.add(addButton);
        add(buttonWrapper, BorderLayout.SOUTH);
    }

    private void showAddUserDialog(ActionEvent e) {
        SwingUtilities.invokeLater(() -> new AddUserDialog(
                (JFrame) SwingUtilities.getWindowAncestor(this),
                (username, password,level) -> tableComponents.addUser(username, password, level)
        ).setVisible(true));
    }

}
